package com.fili;

import com.fili.core.UscatorData;
import com.fili.core.UscatorView;
import com.fili.core.UscatorViewISPM;
import com.fili.gui.ActionsEngine;
import com.fili.gui.components.PanelGrid;
import org.jdesktop.jdic.tray.SystemTray;
import org.jdesktop.jdic.tray.TrayIcon;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

/**
 * Date: Sep 10, 2006
 * Time: 10:11:20 AM
 *
 * @author Claudiu-Florin FILIMON for Borux Consulting S.R.L.
 */
public class JUscator extends JFrame
{
    SystemTray tray = SystemTray.getDefaultSystemTray();
    TrayIcon icon;
    ActionsEngine actionsEngine;

    private JMenuBar menuBar;
    private JTabbedPane tabbedPane;

/*-------------------------------------------------------------------------------------
   Getters and setters
--------------------------------------------------------------------------------------*/

/*-------------------------------------------------------------------------------------
	Constructors
 --------------------------------------------------------------------------------------*/

    public JUscator()
    {
        actionsEngine = new ActionsEngine(this);
        JUscatorActions.setupActions(actionsEngine);

        setSize(400, 300);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

        setupIcon();

        menuBar = new JMenuBar();
        JMenu menu = new JMenu("Fisier");
        menu.add(actionsEngine.getAction(JUscatorActions.ACTION_EXIT));
        menuBar.add(menu);

        setJMenuBar(menuBar);

        PanelGrid pnl = new PanelGrid(4, 2, new UscatorView[]{
                new UscatorViewISPM(new UscatorData(1, "")),
                new UscatorViewISPM(new UscatorData(2, "")),
                new UscatorViewISPM(new UscatorData(3, "")),
                new UscatorViewISPM(new UscatorData(4, "")),
                new UscatorViewISPM(new UscatorData(5, "")),
                new UscatorViewISPM(new UscatorData(6, "")),
                new UscatorViewISPM(new UscatorData(7, "")),
                new UscatorViewISPM(new UscatorData(8, ""))
        });

        tabbedPane = new JTabbedPane();
        tabbedPane.addTab("Uscatoare", new JScrollPane(pnl));
        tabbedPane.addTab("Date", new JScrollPane(pnl));
        tabbedPane.addTab("Grafice", new JScrollPane(pnl));
        add(tabbedPane);

        showWindow();
    }
/*-------------------------------------------------------------------------------------
    Methods
 --------------------------------------------------------------------------------------*/

    public static void main(String[] args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        new JUscator();
    }

    private void showWindow()
    {
        setVisible(true);
        setState(JFrame.NORMAL);
        tray.removeTrayIcon(icon);
    }

    private void hideWindow()
    {
        setVisible(false);
        tray.addTrayIcon(icon);
    }

    private void setupIcon()
    {
        JPopupMenu pm = new JPopupMenu();

        pm.add(actionsEngine.getAction(JUscatorActions.ACTION_SHOW));
        pm.add(actionsEngine.getAction(JUscatorActions.ACTION_HIDE));
        pm.add(actionsEngine.getAction(JUscatorActions.ACTION_EXIT));

        ImageIcon ico = new ImageIcon("res/img/icon.png");
        icon = new TrayIcon(ico, "Test", pm);
        tray.addTrayIcon(icon);
        icon.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                showWindow();
            }
        });
        System.out.println("Added!");


        addWindowListener(new WindowListener()
        {
            public void windowOpened(WindowEvent e)
            {
            }

            public void windowClosing(WindowEvent e)
            {
//                hideWindow();
                System.exit(1);
            }

            public void windowClosed(WindowEvent e)
            {
            }

            public void windowIconified(WindowEvent e)
            {
                hideWindow();
            }

            public void windowDeiconified(WindowEvent e)
            {
            }

            public void windowActivated(WindowEvent e)
            {
            }

            public void windowDeactivated(WindowEvent e)
            {
            }
        });
    }


    public void do_ACTION_SHOW()
    {
        showWindow();
    }

    public void do_ACTION_HIDE()
    {
        hideWindow();
    }

    public void do_ACTION_EXIT()
    {
        System.exit(0);
    }

/*-------------------------------------------------------------------------------------
    Overrides
 --------------------------------------------------------------------------------------*/

/*-------------------------------------------------------------------------------------
    Implementations 
 --------------------------------------------------------------------------------------*/
}
